package mBeanControl.interfacesImpl;

import java.util.ArrayList;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;

import mBeanControl.interfaces.IJMSServer;
import mBeanControl.interfaces.IJVMRuntime;
import mBeanControl.interfaces.IServer;
import mBeanControl.interfaces.IServerConfiguration;
import mBeanControl.interfaces.IThreads;

public class ServerRuntime implements IServer {
	private ObjectName serverRuntime;

	private MBeanServerConnection connection;

	public ServerRuntime(MBeanServerConnection connection, ObjectName o) {
		this.serverRuntime = o;
		this.connection = connection;
	}

	public String getName() {

		String name = null;
		try {
			name = (String) connection.getAttribute(serverRuntime, "Name");

		} catch (Exception e) {
			e.printStackTrace();
		}

		return name;
	}

	public String getState() {
		String state = null;
		try {
			state = (String) connection.getAttribute(serverRuntime, "State");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return state;
	}

	public ArrayList<IJMSServer> getIJMSServers() {
		ObjectName jmsRT;
		ObjectName[] jmsServRTs;
		ArrayList<IJMSServer> iJMSServers = new ArrayList<IJMSServer>();
		try {
			jmsRT = (ObjectName) connection.getAttribute(serverRuntime,
					"JMSRuntime");
			jmsServRTs = (ObjectName[]) connection.getAttribute(jmsRT,
					"JMSServers");

			for (ObjectName jmsServRTObject : jmsServRTs) {
				JMSServerRuntime jmsServer = new JMSServerRuntime(connection,
						jmsServRTObject);
				iJMSServers.add(jmsServer);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return iJMSServers;
	}

	public Integer getOpenSocketsCurrentCount() {
		try {
			return (Integer) connection.getAttribute(serverRuntime,
					"OpenSocketsCurrentCount");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public IJVMRuntime getIJVMRuntime() {
		try {
			ObjectName jVMRuntimeMBean = (ObjectName) connection.getAttribute(
					serverRuntime, "JVMRuntime");
			JVMRuntime jVM = new JVMRuntime(connection, jVMRuntimeMBean);
			return jVM;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	public IThreads getIThreads() {
		try {
			ObjectName threadPoolRuntime = (ObjectName) connection
					.getAttribute(serverRuntime, "ThreadPoolRuntime");
			ThreadsRuntime threadsRuntime = new ThreadsRuntime(connection,
					threadPoolRuntime);
			return threadsRuntime;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	public IServerConfiguration getIServerConfiguration() {
		try {
			ObjectName serverMbean = (ObjectName) connection.getAttribute(
					serverRuntime, "ServerConfiguration");
			ServerConfiguration serverConfiguration = new ServerConfiguration(
					connection, serverMbean);
			return serverConfiguration;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	public IJMSServer getIJMServer(String name) {
		for(IJMSServer jMSServer: getIJMSServers()){
			if(jMSServer.getName().equals(name)){
				return jMSServer;
			}
		}
		return null;
	}

}
